﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormTaiSan : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public FormTaiSan()
        {
            InitializeComponent();
        }
        private int idtam = 0;

        #region Event
        private void BttTaiLai_Click(object sender, EventArgs e)
        {
            GetListTaiSan();
            idtam = 0;
        }

        private void BttTim_Click(object sender, EventArgs e)
        {
            GetListFindName(txtTuKhoa.Text);
        }
        private void dgvTaiSan_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            var row = dgvTaiSan.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells["Id"].Value);
            KiemTra();
        }

        private void dgvTaiSan_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            var row = dgvTaiSan.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells["Id"].Value);
            KiemTra();
            var f = new FormTaiSanCapNhap();
            f.id = idtam;
            f.ShowDialog();
        }
        private void bttLoc_Click(object sender, EventArgs e)
        {
            GetListNhomTaiSanId(Convert.ToInt32(cbbNhomTaiSan.SelectedValue));
        }
        private void BttThemMoi_Click_1(object sender, EventArgs e)
        {
            var t = new FormTaiSanThem();
            t.ShowDialog();
        }

        private void BttChinhSua_Click(object sender, EventArgs e)
        {
            if (idtam != 0)
            {
                var f = new FormTaiSanCapNhap();
                f.id = idtam;
                f.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để chỉnh sửa!");
            }
        }

        private void BttXoa_Click_1(object sender, EventArgs e)
        {
            Delete();
        }
        private void FormTaiSan_Load(object sender, EventArgs e)
        {
            GetListNhomTaiSan();
            GetListTaiSan();
            idtam = 0;
        }
        #endregion Event
        #region Methol

        void GetListTaiSan()
        {
            dgvTaiSan.DataSource = db.TaiSans.ToList();
            KiemTra();
            var n = dgvTaiSan.Columns["NhomTaiSanId"] as DataGridViewComboBoxColumn;
            n.DataSource = db.NhomTaiSans.ToList();
            n.DisplayMember = "TenNhomTaiSan";
            n.ValueMember = "Id";
            var n1 = dgvTaiSan.Columns["DonViTinhId"] as DataGridViewComboBoxColumn;
            if (n1 == null) return;
            n1.DataSource = db.DonViTinhs.ToList();
            n1.DisplayMember = "TenDonViTinh";
            n1.ValueMember = "Id";
        }

        void GetListNhomTaiSan()
        {
            cbbNhomTaiSan.DataSource = db.NhomTaiSans.ToList();
            cbbNhomTaiSan.DisplayMember = "TenNhomTaiSan";
            cbbNhomTaiSan.ValueMember = "Id";
        }

        void Delete()
        {
            if (idtam != 0)
            {
                var d = db.TaiSans.Find(idtam);
                if (
                    MessageBox.Show("Bạn muốn xóa tài sản: " + d.TenTaiSan + "!", "Xác nhận", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    var t = db.TaiSans.Find(idtam);
                    db.TaiSans.Remove(t);
                    db.SaveChanges();
                }
            }
            else
            {
                MessageBox.Show("Ban chưa chọn dữ liệu để xóa!");
            }
            GetListTaiSan();
        }

        void GetListNhomTaiSanId(int id)// get list tai san theo NhomTaiSanId
        {
            dgvTaiSan.DataSource = db.TaiSans.Where(t => t.NhomTaiSanId == id).ToList();
        }

        void GetListFindName(string keyword)
        {
            if (keyword != "")
            {
                dgvTaiSan.DataSource = db.TaiSans.Where(t => t.TenTaiSan.Contains(keyword)).ToList();
            }
            else
            {
                MessageBox.Show("Bạn chưa nhập từ khóa tìm kiếm!");
            }
        }
        void KiemTra()
        {
            if (idtam != 0)
            {
                BttChinhSua.Enabled = true;
                BttXoa.Enabled = true;
            }
            else
            {
                BttChinhSua.Enabled = false;
                BttXoa.Enabled = false;
            }
        }
        #endregion Methol

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
